package com.example.lsy.zyfypt_227lsy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.lsy.zyfypt_227lsy.R;
import com.example.lsy.zyfypt_227lsy.bean.LoginBean;
import com.example.lsy.zyfypt_227lsy.bean.SessionIDBean;
import com.example.lsy.zyfypt_227lsy.iface.LoginListener;
import com.example.lsy.zyfypt_227lsy.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener,LoginListener{

    private EditText username,password;//用户名密码
    private Button login,register;//登录注册按钮
    private LoginModel loginModel;
    private SessionIDBean sessionIDBean;
    private Switch aSwitch;
    private String user="",pass="";
    private SharedPreferences sp;//简单存储对象

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        //获取sp对象
        sp=getSharedPreferences("Save",MODE_PRIVATE);
        //获取
        readSP();
    }

    private void init(){
        username=(EditText)findViewById(R.id.editText);
        password=(EditText)findViewById(R.id.editText2);
        login=(Button)findViewById(R.id.button_login);
        register=(Button)findViewById(R.id.button_regactivity);
        aSwitch=(Switch)findViewById(R.id.switch_login);
        //注册监听器
        login.setOnClickListener(this);
        register.setOnClickListener(this);

    }
    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",user);
        editor.putString("password",pass);
        editor.putBoolean("remember",aSwitch.isChecked());
        editor.commit();
    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            username.setText(name);
            password.setText(pass);
            aSwitch.setChecked(b);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_login:
                loginModel=new LoginModel();
                loginModel.getResultList(username.getText().toString(),password.getText().toString(),this);
                user=username.getText().toString();
                pass=password.getText().toString();
                break;
            case R.id.button_regactivity:
                Intent intent2=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent2);
                break;
        }
    }

    @Override
    public void onResponse(LoginBean beanList) {
        sessionIDBean=new SessionIDBean();
        sessionIDBean.getSessionIDBean=beanList.getSessionid();
        sessionIDBean.realName=beanList.getRealname();
        if(sessionIDBean.getSessionIDBean!=null&&username.length()>0&&password.length()>0){
            saveSP();
            Intent intent3=new Intent(LoginActivity.this,Homepage.class);
            startActivity(intent3);
        }else if(sessionIDBean.getSessionIDBean==null){
            Toast.makeText(this,"请输入正确的用户名密码", Toast.LENGTH_SHORT).show();
        }else if(username.length()==0||  password.length()==0){
            Toast.makeText(this,"请输入用户名和密码", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
